package cn.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WeiboCommentsScraper {

    private static final String header =
            "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
    private static final String userAgent =
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36";
    private static final String cookie =
            "SCF=AjXnBie1oEcMLvztKQ8HlJHhi-frH8WLqOi4PKFXX6yHJKAr2e-W8kloPavVKA6FFL8Xatm_fN1QbqD-XJynYMo.; SUB=_2A25II93KDeRhGeBN6FMZ9CnKzz6IHXVr7-OCrDV6PUNbktANLW33kW1NRGn8RgWLjmRq5Y3QLr8pg5gt66RFnO88; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WF64P3kr1RpR8ZoSdeES4Ui5JpX5KMhUgL.Foq0e02RShMcShz2dJLoI79ZqP9DMNet; WEIBOCN_FROM=1110006030; MLOGIN=1; _T_WM=22737451167; XSRF-TOKEN=dc7fff; mweibo_short_token=5bf28e3837; M_WEIBOCN_PARAMS=oid%3D4974865639540859%26luicode%3D20000061%26lfid%3D4978606294176698%26uicode%3D20000061%26fid%3D4974865639540859";
    private static final String url =
            "https://m.weibo.cn/comments/hotflow?id=4977430797746206&mid=4977430797746206&max_id_type=0";

    private static final String userText = "这是青春";

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        int totalPages = getPages();
        int setPage = 50;
        int crawlPages = Math.min(setPage, totalPages);
        for (int i = 0; i < crawlPages; i++) {
            System.out.println("正在爬取第" + i + 1 + "页");
            String baseUrl =
                    "https://m.weibo.cn/comments/hotflow?id=4977430797746206&mid=4977430797746206&max_id=" +
                            i + "&max_id_type=0";
            JSONObject json = getJson(baseUrl);
            if (json == null) {
                continue;
            }
            String likeCount = jsonParse(json);
            if (likeCount != null) {
                System.out.println("点赞数：" + likeCount);
                break;
            }
        }
        System.out.println(
                "总共耗时： " + (System.currentTimeMillis() - start) / 1000 + " s"
        );
    }

    public static int getPages() throws IOException, JSONException {
        URL url = new URL(WeiboCommentsScraper.url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", userAgent);
        connection.setRequestProperty("Cookie", cookie);
        connection.connect();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream())
        );
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        JSONObject jsonResponse = new JSONObject(response.toString());
        int ok = jsonResponse.getInt("ok");
        if (ok == 1) {
            return jsonResponse.getJSONObject("data").getInt("max");
        } else {
            System.out.println("请求失败");
            return 0;
        }
    }

    public static JSONObject getJson(String url) throws IOException, JSONException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", userAgent);
        con.setRequestProperty("Cookie", cookie);
        con.connect();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream())
        );
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return new JSONObject(response.toString());
    }

//    public static void jsonParse(JSONObject json) throws JSONException {
//        JSONArray data = json.getJSONObject("data").getJSONArray("data");
//        String filePath = "./comments.csv";
//        try (CSVWriter writer = new CSVWriter(new FileWriter(filePath, true))) {
//            for (int i = 0; i < data.length(); i++) {
//                JSONObject item = data.getJSONObject(i);
//                String text = item.getString("text");
//                Pattern pattern = Pattern.compile("[^\\u4e00-\\u9fa5]");
//                Matcher matcher = pattern.matcher(text);
//                String comment = matcher.replaceAll("");
//                matcher = pattern.matcher(comment);
//                comment = matcher.replaceAll("");
////                String comment = re.sub("[^\\u4e00-\\u9fa5]", "", text);
////                comment = re.sub("(<span.*>.*</span>)*(<a.*>.*</a>)?", "", comment);
//                String[] record = {
//                        item.getString("like_count"),
//                        item.getString("created_at"),
//                        item.getJSONObject("user").getString("id"),
//                        item.getJSONObject("user").getString("screen_name"),
//                        comment,
//                };
//                writer.writeNext(record);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static String jsonParse(JSONObject json) throws JSONException {
        JSONArray data = json.getJSONObject("data").getJSONArray("data");
        String likeCount = null;
        for (int i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            String text = item.getString("text");
//            String comment = text.replace("[^\\u4e00-\\u9fa5]", "");
//            comment = text.replace("<span.*>.*</span>)*(<a.*>.*</a>)?", "");
//            Pattern pattern = Pattern.compile("[^\\u4e00-\\u9fa5]");
            Pattern pattern = Pattern.compile("[^\\u4e00-\\u9fa5]");

            Matcher matcher = pattern.matcher(text);
            String comment = matcher.replaceAll("");
//                String comment = re.sub("[^\\u4e00-\\u9fa5]", "", text);
//                comment = re.sub("(<span.*>.*</span>)*(<a.*>.*</a>)?", "", comment);
//            String usertext = userText.replace("[", "").replace("]", "");
            Matcher matcher1 = pattern.matcher(userText);
            String usertext = matcher1.replaceAll("");
            if (comment.equals(usertext)) {
                likeCount = item.getString("like_count");
                break;
            }
//            record[i] = Arrays.toString(new String[]{
//                    item.getString("like_count"),
//                    item.getString("created_at"),
//                    item.getJSONObject("user").getString("id"),
//                    item.getJSONObject("user").getString("screen_name"),
//                    comment,
//            });
        }
        return likeCount;
    }

}
